package com.youko.customerfrontstage.service;

import com.youko.customerfrontstage.bean.*;
import com.youko.customerfrontstage.dto.commodity.CommodityReturnDto;
import com.youko.customerfrontstage.dto.commodity.SKUReturnDto;
import com.youko.customerfrontstage.dto.commodity.SpecOfSpu;
import com.youko.customerfrontstage.dto.commodity.SpecValueDto;
import com.youko.customerfrontstage.mapper.PictureMapper;
import com.youko.customerfrontstage.mapper.SkuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommoditySkuService {
    @Autowired
    SkuMapper skuMapper;
    @Autowired
    PictureMapper pictureMapper;

    /**
     *
     * @param id:首页上点击的上商品的id
     * @return 返回给前端的封装对象，装有前端所需数据
     */
    public CommodityReturnDto selectSpu(int id) {
        CommodityReturnDto commodityReturnDto = new CommodityReturnDto();
        CommoditySpu spu = skuMapper.findOneSpu(id);
        commodityReturnDto.setSpu(spu);
        List<CommoditySku> skus = skuMapper.findSku(spu.getId());
        int stock = 0;
        int sale = 0;
        for (CommoditySku commoditySku : skus) {
            sale+=commoditySku.getSale();
            stock+=commoditySku.getStock();
        }
        commodityReturnDto.setSale(sale);
        commodityReturnDto.setStock(stock);
        List<String> picPath = new ArrayList<>();
        List<PicturePath> picturePaths= pictureMapper.getPic(spu.getId());
        for (PicturePath picturePath : picturePaths) {
            String picP =picturePath.getPicPath();
            picPath.add(picP);
        }
        commodityReturnDto.setPicPath(picPath);
        return commodityReturnDto;
    }

    public List<SKUReturnDto> getReturnSku(int spuID){
        List<CommoditySku> skus = skuMapper.findSku(spuID);
        List<SKUReturnDto> skuReturnDtos = new ArrayList<>();
        for (CommoditySku commoditySku : skus) {
            int idOfSku = commoditySku.getId();
            List<CommoditySkuSpecValue> ssv = skuMapper.findSSV(idOfSku);
            SKUReturnDto skuReturnDto = new SKUReturnDto();
            skuReturnDto.setSku(commoditySku);
            skuReturnDto.setSsv(ssv);
            skuReturnDtos.add(skuReturnDto);
        }

        return skuReturnDtos;
    }

    public SpecValueDto getSpecValueOfSpu(int spuID){
        List<SpecOfSpu> specValueDtos = new ArrayList<>();
        List<CommoditySpuSpec> ss = skuMapper.findSS(spuID);
        List<CommoditySpec> cs = new ArrayList<>();
        for (CommoditySpuSpec s : ss) {
            cs.add(skuMapper.findSpec(s.getSpecID()));
        }

        List<CommoditySpecValue> commoditySpecValues = new ArrayList<>();
        for (CommoditySpec c : cs) {
            SpecOfSpu specOfSpu = new SpecOfSpu();
            specOfSpu.setSpecID(c.getId());
            specOfSpu.setSpec(c.getSpecName());
            specValueDtos.add(specOfSpu);
            List<CommoditySpecValue> sv = skuMapper.findSV(c.getId());
            commoditySpecValues.addAll(sv);
        }
        SpecValueDto specValueDto = new SpecValueDto();
        specValueDto.setSpecOfSpu(specValueDtos);
        specValueDto.setSpecValues(commoditySpecValues);
        return specValueDto;
    }

//    public List<SpecOfSpu> getSpecList(int spuID){
//        List<SpecOfSpu> specValueDtos = new ArrayList<>();
//        List<CommoditySpuSpec> ss = skuMapper.findSS(spuID);
//        List<CommoditySpec> cs = new ArrayList<>();
//        for (CommoditySpuSpec s : ss) {
//            cs.add(skuMapper.findSpec(s.getSpecID()));
//        }
//        List<CommoditySpecValue> commoditySpecValues = new ArrayList<>();
//        for (CommoditySpec c : cs) {
//            SpecOfSpu specOfSpu = new SpecOfSpu();
//            specOfSpu.setSpecID(c.getId());
//            specOfSpu.setSpec(c.getSpecName());
//            specValueDtos.add(specOfSpu);
//            List<CommoditySpecValue> sv = skuMapper.findSV(c.getId());
//            commoditySpecValues.addAll(sv);
//        }
//
//        return specValueDtos;
//    }


}
